package com.lannuokeji.generator.fromJson;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.junit.Test;

import com.alibaba.druid.support.json.JSONParser;

public class Connect {
	static Connection con;
	public static final String url = "jdbc:mysql://192.168.128.52:3306/items2";
	public static final String name = "com.mysql.jdbc.Driver";
	public static final String user = "to";
	public static final String password = "together";

	public static Connection getConnection() {
		try {
			Class.forName(name);
			con = DriverManager.getConnection(url, user, password);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	@Test
	public void test() throws SQLException {
		String jsonText = readToString("D:\\project\\实验室管理系统\\menu.json");
		List<Object> m = new JSONParser(jsonText).parseArray();
		
		Statement stmt=getConnection().createStatement();
		stmt.execute("insert into g_ac_resources (`name`) values('你好')", Statement.RETURN_GENERATED_KEYS);
		ResultSet rs = stmt.getGeneratedKeys();
		if (rs.next()) {
			System.out.println(rs.getInt(1));
		}
	}
	
	public static String readToString(String fileName) {  
        String encoding = "UTF-8";  
        File file = new File(fileName);  
        Long filelength = file.length();  
        byte[] filecontent = new byte[filelength.intValue()];  
        try {  
            FileInputStream in = new FileInputStream(file);  
            in.read(filecontent);  
            in.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        try {  
            return new String(filecontent, encoding);  
        } catch (UnsupportedEncodingException e) {  
            System.err.println("The OS does not support " + encoding);  
            e.printStackTrace();  
            return null;  
        }  
    }
}